//
//  ViewController.m
//  PureLayoutDemo
//
//  Created by Wythe Zhou on 3/2/15.
//  Copyright (c) 2015 demo. All rights reserved.
//

#import "ViewController.h"
#import <PureLayout/PureLayout.h>

@interface ViewController ()

@property (nonatomic, strong) UIView *halfBackgroundView;
@property (nonatomic, strong) UIView *logoView;
@property (nonatomic, strong) UIView *loginBlockView;
@property (nonatomic, strong) UIView *userNameView;
@property (nonatomic, strong) UIView *passwordView;
@property (nonatomic, strong) UIView *loginView;
@property (nonatomic, strong) UIView *forgetPWRigisterView;
@property (nonatomic, strong) UIView *copyrightView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor cyanColor];
    [self addSomeViews];
    [self.view setNeedsUpdateConstraints];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)addSomeViews
{
    [self.view addSubview:self.halfBackgroundView];
    [self.halfBackgroundView addSubview:self.logoView];
    [self.view addSubview:self.loginBlockView];
    [self.loginBlockView addSubview:self.userNameView];
    [self.loginBlockView addSubview:self.passwordView];
    [self.loginBlockView addSubview:self.loginView];
    [self.view addSubview:self.forgetPWRigisterView];
    [self.view addSubview:self.copyrightView];
}

- (void)updateViewConstraints {
    // 边界对齐
    UIEdgeInsets edgeInsets = UIEdgeInsetsZero;
    [self.halfBackgroundView autoPinEdgesToSuperviewEdgesWithInsets:edgeInsets excludingEdge:ALEdgeBottom];
    [self.halfBackgroundView autoConstrainAttribute:ALAttributeHeight toAttribute:ALAttributeHeight ofView:self.view withOffset:-235];
    
    // 设置在中间
    [self.logoView autoCenterInSuperview];
    // 设置固定大小
    [self.logoView autoSetDimensionsToSize:CGSizeMake(100, 100)];
    
    // X 中间
    [self.loginBlockView autoAlignAxisToSuperviewAxis:ALAxisVertical];
    [self.loginBlockView autoConstrainAttribute:ALAttributeTop toAttribute:ALAttributeBottom ofView:self.halfBackgroundView withMultiplier:0.75];
    
    // 固定高度
    [self.loginBlockView autoSetDimension:ALDimensionHeight toSize:170];
    // 和左右的距离
    [self.loginBlockView autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:25];
    [self.loginBlockView autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:25];
    [super updateViewConstraints];
    
    // 左右距离
    [self.userNameView autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:15];
    [self.userNameView autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:15];
    // 顶部距离
    [self.userNameView autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:10];
    // 自身高度
    [self.userNameView autoSetDimension:ALDimensionHeight toSize:35];
    
    // 左右距离
    [self.passwordView autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:15];
    [self.passwordView autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:15];
    // 顶部距离
    [self.passwordView autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.userNameView withOffset:15];
    // 自身高度
    [self.passwordView autoSetDimension:ALDimensionHeight toSize:35];
    
    // 左右距离
    [self.loginView autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:10];
    [self.loginView autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:10];
    // 顶部距离
    [self.loginView autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.passwordView withOffset:15];
    // 自身高度
    [self.loginView autoSetDimension:ALDimensionHeight toSize:50];
    
    // 自身尺寸
    [self.copyrightView autoSetDimensionsToSize:CGSizeMake(200, 50)];
    // X 居中
    [self.copyrightView autoAlignAxisToSuperviewAxis:ALAxisVertical];
    // 距离底部尺寸
    [self.copyrightView autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:10];
    
    // 自身尺寸
    [self.forgetPWRigisterView autoSetDimension:ALDimensionHeight toSize:30];
    // 左右间距
    [self.forgetPWRigisterView autoPinEdgeToSuperviewEdge:ALEdgeLeft];
    [self.forgetPWRigisterView autoPinEdgeToSuperviewEdge:ALEdgeRight];
    // 距离底部尺寸
    [self.forgetPWRigisterView autoConstrainAttribute:ALAttributeBottom toAttribute:ALAttributeBottom ofView:self.view withMultiplier:0.85];
}

- (UIView *)halfBackgroundView {
    if (!_halfBackgroundView) {
        _halfBackgroundView = [UIView newAutoLayoutView];
        _halfBackgroundView.backgroundColor = [UIColor greenColor];
    }
    return _halfBackgroundView;
}

- (UIView *)logoView {
    if (!_logoView) {
        _logoView = [UIView newAutoLayoutView];
        _logoView.backgroundColor = [UIColor lightGrayColor];
    }
    return _logoView;
}

- (UIView *)loginBlockView {
    if (!_loginBlockView) {
        _loginBlockView = [UIView newAutoLayoutView];
        _loginBlockView.backgroundColor = [UIColor redColor];
    }
    return _loginBlockView;
}

- (UIView *)userNameView {
    if (!_userNameView) {
        _userNameView = [UIView newAutoLayoutView];
        _userNameView.backgroundColor = [UIColor whiteColor];
    }
    return _userNameView;
}

- (UIView *)passwordView {
    if (!_passwordView) {
        _passwordView = [UIView newAutoLayoutView];
        _passwordView.backgroundColor = [UIColor purpleColor];
    }
    return _passwordView;
}

- (UIView *)loginView {
    if (!_loginView) {
        _loginView = [UIView newAutoLayoutView];
        _loginView.backgroundColor = [UIColor yellowColor];
    }
    return _loginView;
}

- (UIView *)forgetPWRigisterView {
    if (!_forgetPWRigisterView) {
        _forgetPWRigisterView = [UIView newAutoLayoutView];
        _forgetPWRigisterView.backgroundColor = [UIColor orangeColor];
    }
    return _forgetPWRigisterView;
}

- (UIView *)copyrightView {
    if (!_copyrightView) {
        _copyrightView = [UIView newAutoLayoutView];
        _copyrightView.backgroundColor = [UIColor greenColor];
    }
    return _copyrightView;
}

@end
